<?php
/**
 * Created by PhpStorm.
 * User: yangwang
 * Date: 2024-04-17
 * Time: 14:24
 */

namespace App\Kuafu\Shop\Service;


use App\Kuafu\Base\Service\BaseService;
use App\Kuafu\Shop\Model\ShopStaffRecordLogModel;

class ShopStaffRecordLogService extends BaseService
{

    /**
     * @param int $shopId
     * @param int $shopCode
     * @param int $userId
     * @param int $operateUserId
     * @param int $operateType
     * @param int $actionType
     * @param string $remark
     * @return int
     * @throws \Exception
     */
    public static function addRecord(
        int $shopId,
        int $shopCode,
        int $userId,
        int $operateUserId,
        int $operateType,
        int $actionType,
        string $remark = ''
    ) {
        if (!in_array($operateType, array_keys(ShopStaffRecordLogModel::OPERATE_TYPE_MAP))) {
            throw new \Exception('操作类型不可枚举');
        }

        if (!in_array($actionType, array_keys(ShopStaffRecordLogModel::ACTION_TYPE_MAP))) {
            throw new \Exception('行为类型不可枚举');
        }

        return ShopStaffRecordLogModel::query()->insertGetId([
            'shopId'       => $shopId,
            'shopCode'     => $shopCode,
            'userId'       => $userId,
            'operateType'  => $operateType,
            'actionType'   => $actionType,
            'remark'       => $remark,
            'createdByUid' => $operateUserId,
            'updatedByUid' => $operateUserId,
            'createdAt'    => time(),
            'updatedAt'    => time(),
        ]);
    }

    public static function batchAddRecord(array $list)
    {
        return ShopStaffRecordLogModel::query()->insert($list);
    }

    /**
     * @param int $userId
     * @param int $shopId
     * @param int $pageSize
     * @param int $pageNum
     * @return array
     */
    public static function getLogList(int $userId, int $shopId, int $pageSize, int $pageNum)
    {
        $paginate = ShopStaffRecordLogModel::query()
                                           ->where('userId', $userId)
                                           ->where('shopId', $shopId)
                                           ->orderBy('id', 'desc')
                                           ->paginate($pageSize, ['*'], $pageName = 'page', $pageNum);
        return [
            'pageSize' => $paginate->perPage(),
            'pageNum'  => $paginate->currentPage(),
            'count'    => $paginate->total(),
            'list'     => collect($paginate->items())
                ->map(function ($item) {
                    $createdUsername = $item->createdUserRel->username ?? '未记录';
                    return [
                        'createdAt'   => $item->createdAt->format('Y-m-d H:i:s'),
                        'operateDesc' => ShopStaffRecordLogModel::OPERATE_TYPE_MAP[$item->operateType] . ' : ' . $createdUsername,
                        'actionDesc'  => ShopStaffRecordLogModel::ACTION_TYPE_MAP[$item->actionType],
                        'remark'      => $item->remark ?: '-'
                    ];
                })
                ->toArray() ?: []
        ];

    }

}